package com.jdz.noise.tools;
import com.jdz.noise.entities.FlyRow;
import com.jdz.noise.entities.NoiseRow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static com.jdz.noise.tools.ControlUtil.*;
public class Merge {

    public static int getNum(double noiseFreq,double step)
    {
        double stepNoise = 1/noiseFreq;
        int num = (int) (step/stepNoise);
        return num;
    }

    public static Map<NoiseRow, FlyRow> avg(int num)//num是几个一合并，等于步长/噪声检测步长
    {
        Map<NoiseRow, FlyRow> avg = new HashMap<NoiseRow, FlyRow>();
        int t=0;
        double avgTime = 0;
        ArrayList<Double> avgNoiseArray = new ArrayList<Double>();
        double len = 3;
        for(int i=0;i<len;i++)
        {
            avgNoiseArray.add(0.0);
        }
        for(NoiseRow noiseRow:noise_fly.keySet())
        {
            t++;
            if(t<num)//还没攒够
            {
                //NoiseRow time和ArrayList<DOUBLE>
                avgTime += noiseRow.getTime();
                for(int i=0;i<len;i++)
                {
                    double p = avgNoiseArray.get(i);
                    double q = noiseRow.getVocal_tract().get(i);
                    avgNoiseArray.set(i,p+q);
                }
                //FlyRow

            }
            else//够num了
            {

                avgTime = avgTime/num;
                for(int i=0;i<len;i++) {
                    avgNoiseArray.set(i,avgNoiseArray.get(i)/num);
                }
                NoiseRow avgNoiseRow = new NoiseRow(avgTime,avgNoiseArray);

                avg.put(avgNoiseRow,noise_fly.get(noiseRow));
                t=0;
                avgTime=0;
                for(int i=0;i<len;i++) {
                    avgNoiseArray.set(i,0.0);
                }
            }
        }
        return avg;

    }


}
